﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using RuleCode.Table;
using System.Data;
using RuleCode.Common;

namespace map.member
{
    public partial class FriendGroups : System.Web.UI.Page
    {
        #region 载入
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFriendGroupListView();
                //首次打开全部分组
                AllGroupAnchor_ServerClick(this.G_AllGroupAnchor, EventArgs.Empty);
            }
        }
        #endregion

        #region 辅助方法
        private void BindFriendGroupListView()
        {
            if (AliApi.appUserId != string.Empty)
            {
                cbusergroup usergroup = cbusergroup.Find("cbuserid='"+AliApi.appUserId +"'");
                this.FriendGroupDataList.DataSource = usergroup.MyTable;
                this.FriendGroupDataList.DataBind();
            }
        }

        private void BindMainDataList(string groupid)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string SqlText = " exec [GetFriends] @userid='" + AliApi.appUserId + "' ";
                if (groupid != string.Empty)
                    SqlText += " , @usergroupid='" + groupid + "'";
                DataSet dSet = SqlHelper.Instance.ExecuteQuery(SqlText );
                if (dSet.Tables.Count > 0)
                {
                    this.MainWebPager .DataSource = dSet.Tables[0];
                    this.MainWebPager.DataBind();
                }  
            }
            NoDataDiv.Visible = (this.MainDataList.Items.Count == 0);
        }
        private void ClearSelectedGroup()
        {
            G_AllGroupAnchor.Style[HtmlTextWriterStyle.BackgroundColor] = "white";
            for (int i = 0; i < this.FriendGroupDataList.Items.Count; i++)
            {
                HtmlAnchor anchor = this.FriendGroupDataList.Items[i].FindControl("G_GroupNameAnchor") as HtmlAnchor;
                anchor.Style[HtmlTextWriterStyle.BackgroundColor] = "white";
            }
        }
        #endregion

        #region 事件    
        protected void AllGroupAnchor_ServerClick(object sender, EventArgs e)
        {
            ClearSelectedGroup();
            BindMainDataList("");
            G_AllGroupAnchor.Style[HtmlTextWriterStyle.BackgroundColor] = "lightBlue";
            CurrentFriendGroupAnchor.InnerHtml = G_AllGroupAnchor.InnerHtml;
            ViewState["SelectedGroupId"] = "";
        }
        
        protected void DataListAnchor_ServerClick(object sender, EventArgs e)
        {
            ClearSelectedGroup();
            HtmlInputHidden hidden = ((sender as Control).Parent.FindControl("FriendGroupIdHidden") as HtmlInputHidden);
            HtmlAnchor anchor = sender as HtmlAnchor;
            string usergroupid = hidden.Value ;
            BindMainDataList(usergroupid);
            anchor.Style[HtmlTextWriterStyle.BackgroundColor] = "lightBlue";
            CurrentFriendGroupAnchor.InnerHtml = anchor.InnerHtml;
            ViewState["SelectedGroupId"] = usergroupid;
        }

        protected void UpdatePageButton_Click(object sender, EventArgs e)
        {
            BindFriendGroupListView();
            if(AliApi.appUserId == string.Empty )
                return ;

           
            if (ViewState["SelectedGroupId"] == null)
            {
                AllGroupAnchor_ServerClick(this.G_AllGroupAnchor, EventArgs.Empty);
            }
            else
            {
                if (ViewState["SelectedGroupId"].ToString() == "")
                {
                    AllGroupAnchor_ServerClick(this.G_AllGroupAnchor, EventArgs.Empty);
                }
                else
                {
                    cbusergroup usergroup = cbusergroup.Find("cbuserid='"+AliApi.appUserId +"' and cbusergroupid='" + ViewState["SelectedGroupId"].ToString() + "'");
                    if (usergroup.Count > 0)
                    {
                        for (int i = 0; i < this.FriendGroupDataList.Items.Count; i++)
                        {
                            HtmlAnchor anhor = this.FriendGroupDataList.Items[i].FindControl("G_GroupNameAnchor") as HtmlAnchor;
                            HtmlInputHidden hidden = (anhor.Parent.FindControl("FriendGroupIdHidden") as HtmlInputHidden);
                            if (ViewState["SelectedGroupId"].ToString() == hidden.Value)
                            {
                                DataListAnchor_ServerClick(anhor, EventArgs.Empty);
                                return;
                            }
                        }
                    }
                    else
                    {
                        AllGroupAnchor_ServerClick(this.G_AllGroupAnchor, EventArgs.Empty);
                    }
                }
            }

            //ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "updatedd", "<script type=\"text/javascript\">initFriendGroupDatalList();</script>", false);
        }

        protected void MainWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            this.MainWebPager.CurrentPageIndex = e.NewPageIndex;
            UpdatePageButton_Click(MainWebPager ,EventArgs .Empty );
        }
        #endregion
    }
}
